<template>
  <div>
    <van-nav-bar
      title="个人信息"
      left-arrow
      right-text="保存"
      @click-left="$router.back()"
    />
    <van-cell-group>
      <!-- 上传框 -->
      <input type="file" hidden ref="file" @change="onFileChange" />
      <van-cell title="头像" is-link @click="$refs.file.click()">
        <van-image round width="30" height="30" fit="cover" :src="user.photo" />
      </van-cell>
      <van-cell
        title="昵称"
        :value="user.name"
        @click="isUpdateNameShow = true"
        is-link
      />
      <!-- 编辑昵称 -->
      <van-popup
        v-model="isUpdateNameShow"
        style="height: 100%"
        position="bottom"
      >
        <update-name
          v-if="isUpdateNameShow"
          v-model="user.name"
          @close="isUpdateNameShow = false"
        />
      </van-popup>

      <van-cell
        @click="isUpdateGenderShow = true"
        title="性别"
        :value="user.gender ? '女' : '男'"
        is-link
      />
      <!-- 编辑性别 -->
      <van-popup v-model="isUpdateGenderShow" position="bottom">
        <update-gender
          v-if="isUpdateGenderShow"
          v-model="user.gender"
          @close="isUpdateGenderShow = false"
        />
      </van-popup>
      <van-cell
        title="生日"
        :value="user.birthday"
        is-link
        @click="isUpdateBirthdayShow = true"
      />
      <!-- 编辑生日 -->
      <van-popup v-model="isUpdateBirthdayShow" position="bottom">
        <update-birthday
          v-if="isUpdateBirthdayShow"
          v-model="user.birthday"
          @close="isUpdateBirthdayShow = false"
        />
      </van-popup>

      <!-- 编辑头像 -->
      <van-popup
        v-model="isUpdatePhotoShow"
        position="bottom"
        style="height: 100%;"
      >
        <update-photo
          v-if="isUpdatePhotoShow"
          :img="img"
          @close="isUpdatePhotoShow = false"
          @update-photo="user.photo = $event"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'
export default {
  name: 'UserIndex',
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        console.log(err)
      }
    },
    onFileChange () {
      // 展示预览图片弹出层
      this.isUpdatePhotoShow = true
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
.van-popup {
  background-color: #f5f7f9;
}
</style>
